﻿namespace PMS.Client.FrmJobTitle
{
    using AdvancedDataGridView;
    using PMS.BLL;
    using PMS.Client.Properties;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    public class FrmList : Form
    {
        private ToolStripButton btnAdd;
        private ToolStripButton btnDelete;
        private ToolStripButton btnUpdate;
        private IContainer components = null;
        private JobTitleBLL titleBLL = new JobTitleBLL();
        private DataGridViewTextBoxColumn titleID;
        private TreeGridColumn titleName;
        private JobTitleTypeBLL titleTypeBLL = new JobTitleTypeBLL();
        private ToolStrip toolStrip1;
        private TreeGridView treeGridView1;
        private DataGridViewTextBoxColumn typeID;

        public FrmList()
        {
            this.InitializeComponent();
            this.LoadJobTitle2();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmAdd add = new FrmAdd();
            add.ShowDialog();
            if (add.DialogResult == DialogResult.OK)
            {
                this.LoadJobTitle2();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if ((this.treeGridView1.SelectedRows.Count <= 0) || (this.treeGridView1.SelectedRows[0].Cells["titleID"].Value == null))
            {
                MessageBox.Show("请选择要删除的职称！");
            }
            else if (MessageBox.Show("确定删除该职称？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
            {
                int titleID = int.Parse(this.treeGridView1.SelectedRows[0].Cells["titleID"].Value.ToString());
                if (!this.titleBLL.Delete(titleID))
                {
                    MessageBox.Show("删除失败！");
                }
                else
                {
                    this.LoadJobTitle2();
                }
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if ((this.treeGridView1.SelectedRows.Count <= 0) || (this.treeGridView1.SelectedRows[0].Cells["titleID"].Value == null))
            {
                MessageBox.Show("请选择要更新的职称！");
            }
            else
            {
                FrmUpdate update = new FrmUpdate(int.Parse(this.treeGridView1.SelectedRows[0].Cells["titleID"].Value.ToString()));
                update.ShowDialog();
                if (update.DialogResult == DialogResult.OK)
                {
                    this.LoadJobTitle2();
                }
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmList));
            this.toolStrip1 = new ToolStrip();
            this.btnAdd = new ToolStripButton();
            this.btnUpdate = new ToolStripButton();
            this.btnDelete = new ToolStripButton();
            this.treeGridView1 = new TreeGridView();
            this.titleName = new TreeGridColumn();
            this.typeID = new DataGridViewTextBoxColumn();
            this.titleID = new DataGridViewTextBoxColumn();
            this.toolStrip1.SuspendLayout();
            ((ISupportInitialize) this.treeGridView1).BeginInit();
            base.SuspendLayout();
            this.toolStrip1.Items.AddRange(new ToolStripItem[] { this.btnAdd, this.btnUpdate, this.btnDelete });
            this.toolStrip1.Location = new Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.RenderMode = ToolStripRenderMode.System;
            this.toolStrip1.Size = new Size(0x161, 0x36);
            this.toolStrip1.TabIndex = 3;
            this.toolStrip1.Text = "toolStrip1";
            this.btnAdd.Image = Resources.Icons_add;
            this.btnAdd.ImageScaling = ToolStripItemImageScaling.None;
            this.btnAdd.ImageTransparentColor = Color.Magenta;
            this.btnAdd.Name = "btnAdd";
            this.btnAdd.Size = new Size(0x24, 0x33);
            this.btnAdd.Text = "增加";
            this.btnAdd.TextImageRelation = TextImageRelation.ImageAboveText;
            this.btnAdd.Click += new EventHandler(this.btnAdd_Click);
            this.btnUpdate.Image = Resources.Icons_update;
            this.btnUpdate.ImageScaling = ToolStripItemImageScaling.None;
            this.btnUpdate.ImageTransparentColor = Color.Magenta;
            this.btnUpdate.Name = "btnUpdate";
            this.btnUpdate.Size = new Size(0x24, 0x33);
            this.btnUpdate.Text = "修改";
            this.btnUpdate.TextImageRelation = TextImageRelation.ImageAboveText;
            this.btnUpdate.Click += new EventHandler(this.btnUpdate_Click);
            this.btnDelete.Image = (Image) manager.GetObject("btnDelete.Image");
            this.btnDelete.ImageScaling = ToolStripItemImageScaling.None;
            this.btnDelete.ImageTransparentColor = Color.Magenta;
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Size = new Size(0x24, 0x33);
            this.btnDelete.Text = "删除";
            this.btnDelete.TextImageRelation = TextImageRelation.ImageAboveText;
            this.btnDelete.Click += new EventHandler(this.btnDelete_Click);
            this.treeGridView1.AllowUserToAddRows = false;
            this.treeGridView1.AllowUserToDeleteRows = false;
            this.treeGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.treeGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            this.treeGridView1.BackgroundColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            this.treeGridView1.Columns.AddRange(new DataGridViewColumn[] { this.titleName, this.typeID, this.titleID });
            this.treeGridView1.Dock = DockStyle.Fill;
            this.treeGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
            this.treeGridView1.EnableHeadersVisualStyles = false;
            this.treeGridView1.ImageList = null;
            this.treeGridView1.Location = new Point(0, 0x36);
            this.treeGridView1.Name = "treeGridView1";
            this.treeGridView1.RowHeadersVisible = false;
            this.treeGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.treeGridView1.Size = new Size(0x161, 0x19d);
            this.treeGridView1.TabIndex = 5;
            this.titleName.DefaultNodeImage = null;
            this.titleName.FillWeight = 80f;
            this.titleName.HeaderText = "职称列表";
            this.titleName.Name = "titleName";
            this.titleName.Resizable = DataGridViewTriState.True;
            this.titleName.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.typeID.HeaderText = "typeID";
            this.typeID.Name = "typeID";
            this.typeID.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.typeID.Visible = false;
            this.titleID.HeaderText = "titleID";
            this.titleID.Name = "titleID";
            this.titleID.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.titleID.Visible = false;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x161, 0x1d3);
            base.Controls.Add(this.treeGridView1);
            base.Controls.Add(this.toolStrip1);
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.Name = "FrmList";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "职称列表";
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            ((ISupportInitialize) this.treeGridView1).EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void LoadJobTitle2()
        {
            Font font = new Font(this.treeGridView1.DefaultCellStyle.Font, FontStyle.Bold);
            List<UserJobTitleType> list = this.titleTypeBLL.GetList();
            List<UserJobTitle> list2 = this.titleBLL.GetList();
            this.treeGridView1.Nodes.Clear();
            using (List<UserJobTitleType>.Enumerator enumerator = list.GetEnumerator())
            {
                Predicate<UserJobTitle> match = null;
                UserJobTitleType type;
                while (enumerator.MoveNext())
                {
                    type = enumerator.Current;
                    object[] values = new object[3];
                    values[0] = type.TypeName;
                    values[1] = type.TypeID;
                    TreeGridNode node = this.treeGridView1.Nodes.Add(values);
                    node.DefaultCellStyle.Font = font;
                    node.DefaultCellStyle.ForeColor = Color.White;
                    node.DefaultCellStyle.BackColor = SystemColors.Highlight;
                    node.Expand();
                    if (match == null)
                    {
                        match = f => f.TitleTypeID == type.TypeID;
                    }
                    List<UserJobTitle> list3 = list2.FindAll(match);
                    if (list3.Count > 0)
                    {
                        foreach (UserJobTitle title in list3)
                        {
                            node.Nodes.Add(new object[] { title.TitleName, title.TitleTypeID, title.TitleID }).DefaultCellStyle.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
                        }
                    }
                }
            }
        }
    }
}

